<?php

namespace Home\Controller;

use Think\Controller;
/**
 * 外包客服外呼中心
 */
class UserQuotaSurveyController extends CommnController
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 用户提额调查
     */
    public function cert_user_list()
    {
        $type=I('type',1);
        $page_first = I('get.page_first');
        $search = I('get.user');
        $this->rid = I('rid');
        $this->roles = role(I('rid'));
        $this->page = I('page', 0);
        $this->user_content = $search;
        if ($_SESSION['role_id'] != '3' && $_SESSION['role_id'] != '11' && $_SESSION['role_id'] != '13') {
            $where['b.out_kf_id'] = array('eq', $_SESSION['id']);
        }
        if($type==2){
            $where['b.status']=array('eq',4);
        }else{
            $where['b.status']=array('neq',4);
        }

        $where['a.mobile|a.truename'] = array('like', '%' . $search . '%');
        $tol = M('cert_check_user', '', 'KEFU')->join('b join dxd_user.user a on b.vid=a.vid')->where($where)->count();
        $row = '10';
        $page = new PageController($tol, $row);
        $fpage = $page->fpage();
        if (!empty($page_first)) {
            $page->listfirst = ($this->page-1)*10;
        }
        $field = 'a.truename as name,a.mobile,a.sex,b.status,a.cert_time,a.vid,c.name as admin_name ';
        $user = M('cert_check_user', '', 'KEFU')->join('b join dxd_user.user a on b.vid=a.vid')->join('dxd_kefu.admin c on b.out_kf_id=c.id')->where($where)->LIMIT($page->listfirst, $page->listRows)->field($field)->select();
        foreach ($user as $k => $v) {

            //用户评分
            $user[$k]['logic_score'] = M('user_good_bad', '', 'USER')->where(['vid' => $v['vid']])->getfield('logic_level');

            if ($v['cert_time'] != 0) {
                $user[$k]['cert_time'] = date('Y-m-d H:i', $v['cert_time']);
            } else {
                $user[$k]['cert_time'] = '-';
            }

            if ($v['sex'] == 1) {
                $user[$k]['sex'] = '男';
            } else if ($v['sex'] == 2) {
                $user[$k]['sex'] = '女';
            } else {
                $user[$k]['sex'] = '未知';
            }

            if ($v['status'] == 2) {
                $user[$k]['status'] = '未开始';
            } elseif ($v['status'] == 1) {
                $user[$k]['status'] = '正在调查';
            } else if ($v['status'] == 3) {
                $user[$k]['status'] = '有记录';
            } else if ($v['status'] == 4) {
                $user[$k]['status'] = '调查结束';
            }
        }

        $this->assign(array(
            'user' => $user,
            'fpage' => $fpage,
            'page_first' => $page->listfirst,
            'tol' => $tol,
            'page' => $this->page,
            'type' => $type,
            'admin' => $_SESSION['role_id']
        ));
        $this->display();
    }

    /**
     * 用户详情
     */
    public function user_info()
    {
        $type=I('type',1);
        $page11 = I('page11', 0);
        $page_first = I('get.page_first11', 0);
        $vid = I('get.vid');
        //修改调查状态
        $status=  M('cert_check_user', '', 'KEFU')->where(['vid' => $vid])->getField('status');
        if($status=='2'){
            M('cert_check_user', '', 'KEFU')->where(['vid' => $vid])->setfield('status', 1);
        }
        $field='vid,mobile,sex,ctime,address,residence_time,id_card,shebao_verify,work_verify,taobao_verify,jd_verify,credit_verify,edu_verify,alipay_verify';
        $user = M('user', '', 'USER')->where(['vid' => $vid])->field($field)->find();

        //用户各种认证 社保认证 1：未认证 2：认证中 3认证成功，4.认证失败
        switch ($user['shebao_verify']){
            case 1: $user['shebao_verify']='未认证';break;
            case 2: $user['shebao_verify']='认证中';break;
            case 3: $user['shebao_verify']='认证成功';break;
            case 4: $user['shebao_verify']='认证失败';break;
        }
        //京东认证1：未认证 2：认证中 3认证成功，4.认证失败
        switch ($user['jd_verify']){
            case 1: $user['jd_verify']='未认证';break;
            case 2: $user['jd_verify']='认证中';break;
            case 3: $user['jd_verify']='认证成功';break;
            case 4: $user['jd_verify']='认证失败';break;
        }
        //工作认证 1认证2未认证
        switch ($user['work_verify']){
            case 1: $user['work_verify']='认证';break;
            case 2: $user['work_verify']='未认证';break;
            case 0: $user['work_verify']='未认证';break;
        }

        //淘宝认证 1：未认证 2：认证中 3认证成功，4.认证失败
        switch ($user['taobao_verify']){
            case 1: $user['taobao_verify']='未认证';break;
            case 2: $user['taobao_verify']='认证中';break;
            case 3: $user['taobao_verify']='认证成功';break;
            case 4: $user['taobao_verify']='认证失败';break;
        }


        //信用卡认证 1：未认证 2：认证中 3认证成功，4.认证失败
        switch ($user['credit_verify']){
            case 1: $user['credit_verify']='未认证';break;
            case 2: $user['credit_verify']='认证中';break;
            case 3: $user['credit_verify']='认证成功';break;
            case 4: $user['credit_verify']='认证失败';break;
        }


        //学历认证1未认证2认证中3已认证
        switch ($user['edu_verify']){
            case 1: $user['edu_verify']='未认证';break;
            case 2: $user['edu_verify']='认证中';break;
            case 3: $user['edu_verify']='认证成功';break;
            case 4: $user['edu_verify']='认证失败';break;
        }

        //支付宝认证1未认证，2认证中，3已认证，4认证失败
        switch ($user['alipay_verify']){
            case 1: $user['alipay_verify']='未认证';break;
            case 2: $user['alipay_verify']='认证中';break;
            case 3: $user['alipay_verify']='认证成功';break;
            case 4: $user['alipay_verify']='认证失败';break;
        }

        $first = substr($user['id_card'], 0, 3);
        $end = substr($user['id_card'], strlen($user['id_card']) - 9, strlen($user['id_card']));
        $user['id_card'] = $first . '******' . $end;

        $user_pic = M('user_idcard_img', '', 'USER')->where(['vid' => $user['vid']])->field('photo,photo1,photo2,front,behind')->find();
        $user['ctime'] = date('Y-m-d H:i', $user['ctime']);
        $user_contact = M('user_contact', '', 'USER')->where(['vid' => $vid])->field('relative_name,relative_mobile,r_type,other_name,other_mobile,f_type')->find();
        $w = [];
        $w['mymobile'] = array('eq', $user['mobile']);
        $w['call_other_number'] = array('eq', $user_contact['relative_mobile']);

        $contact_sum = M('mobile_callrecord_s', '', 'KF_USER_TX')->where($w)->count();
        $contact_sum2 = M('mobile_callrecord_s', '', 'KF_USER_TX')->where(['mymobile' => $user['mobile'], 'call_other_number' => $user_contact['other_mobile']])->count();
        $user_contact['contact_sum'] = $contact_sum;
        $user_contact['contact_sum2'] = $contact_sum2;

        $user['score'] = M('user_good_bad', '', 'USER')->where(['vid' => $vid])->getfield('logic_level');

        //反馈详情
        $feedback = M('tmp_user_mark', '', 'KEFU')->where(['vid' => $vid])->field('content')->limit(1)->find();
        if (!empty($feedback)) {
            $user['feedback_status'] = '1';//已经备注过
        } else {
            $user['feedback_status'] = '0';//没有备注过
        }

        $this->assign(array(
            'user' => $user,
            'user_pic' => $user_pic,
            'user_contact' => $user_contact,
            'vid' => $vid,
            'type' => $type,
            'page11' => $page11,
            'page_first11' => $page_first,
            'role' => $_SESSION['role_id'],
        ));
        $this->display();
    }

    /*
     * 调查完成
     * **/
    public function survey_finish(){
        $vid=I('vid');
        $update= M('cert_check_user', '', 'KEFU')->where(['vid' => $vid])->setfield('status', 4);
        if($update){
            $this->ajaxReturn(['code'=>'1001','result'=>'完成']);
            die;
        }
        $this->ajaxReturn(['code'=>'1002','result'=>'失败']);
        die;
    }

    /**
     * 详情
     */

    public function list_details()
    {
        $vid = I('get.vid');
        $data = M('check_user_mark', '', 'KEFU')->join('b join dxd_user.user a on b.vid=a.vid')->where(['b.vid' => $vid])->field('b.id,b.admin_name,b.content,b.ctime,b.vid,a.truename,a.mobile')->select();

        foreach ($data as $k => $v) {
            $data[$k]['ctime'] = date('Y-m-d H:i', $v['ctime']);
        }

//        $url_type = I('url_type');
//        $type_status = I('type_status');
        $this->page = I('page');
        $this->assign(array(
            'data' => $data,
            'vid' => $vid,
//            'url_type' => $url_type,
//            'type_status' => $type_status
        ));
        $this->display();
    }
    /**
     *提交记录
     */
    public function outbound_result_no()
    {
        $vid = I('vid');
        $content = I('content');
        $arr = array('admin_name' => $_SESSION['name'], 'content' => $content, 'ctime' => time(), 'vid' => $vid);
        $this->page = I('page');
        $add = M('check_user_mark', '', 'KEFU')->add($arr);
        if ($add) {
            $status= M('cert_check_user', '', 'KEFU')->where(['vid' => $vid])->getField('status');
            if($status=='2' || $status=='1'){
                $arr = array('status' => 3, 'update_time' => time());
                M('cert_check_user', '', 'KEFU')->where(['vid' => $vid])->save($arr);
            }
            add_log('对用户' . $vid . '外呼备注cert_check_userID' . $add);
            $this->ajax('1001', '备注成功');
            die;
        }
        $this->ajax('1002', '备注失败');
        die;
    }

    /**
     * 编辑内容
     */
    public function edit_content()
    {

        if (empty($_POST)) {
            $id = I('id');
            $content = M('check_user_mark', '', 'KEFU')->where(['id' => $id])->field('content,vid')->find();
            $this->assign(array(
                'content' => $content['content'],
                'id' => $id,
                'vid' => $content['vid']
            ));

            $this->display();
        } else {
            $id = I('id');
            $content = I('edit_content');
            $info = M('check_user_mark', '', 'KEFU')->where(['id' => $id])->setfield('content', $content);
            if ($info) {
                add_log('修改外呼记录' . $id);
                $this->ajax('1001', '修改成功');
                die;
            }
            $this->ajax('1002', '修改失败');
            die;
        }

    }


}